//
//  DHClearAreaCell.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/14.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHClearAreaCell.h"
#import "DHButtonCell.h"
NSString *buttonCellID = @"buttonCellID";
@interface DHClearAreaCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic ,strong)UICollectionView *clearAreaView;
@property(nonatomic ,strong)UICollectionViewFlowLayout *layout;
@end
@implementation DHClearAreaCell
#pragma mark: - 懒加载 layout
- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
    }
    return _layout;
}
- (void)setClearAreaList:(NSArray<DHClearArea *> *)clearAreaList
{
    _clearAreaList = clearAreaList;
    
    if (!clearAreaList){return;}
    [self setupUIWithArray:clearAreaList];
    //[self.clearAreaView reloadData];
}
#pragma mark: - touchesBegan
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:true];
}
- (void)setupUIWithArray:(NSArray *)array{
    NSInteger numOfRow = (array.count % 3 == 0) ? array.count / 3 : array.count / 3 + 1;
    //设置projectView属性
    self.clearAreaView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    self.clearAreaView.scrollEnabled = NO;
    self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    self.layout.sectionInset = UIEdgeInsetsMake(10, 5,5, 10);
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.itemSize = CGSizeMake((self.bounds.size.width) / 3, (self.bounds.size.height) / numOfRow);
    [self.contentView addSubview:self.clearAreaView];
    self.clearAreaView.backgroundColor = UIColor.whiteColor;
    [self.clearAreaView reloadData];
    
}
#pragma mark: - 懒加载 projectView
- (UICollectionView *)clearAreaView{
    if (!_clearAreaView) {
        _clearAreaView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _clearAreaView.dataSource = self;
        _clearAreaView.delegate = self;
        
    }
    return _clearAreaView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    [self.clearAreaView registerNib:[UINib nibWithNibName:@"DHButtonCell" bundle:nil] forCellWithReuseIdentifier:buttonCellID];

    return [super initWithStyle:style reuseIdentifier:reuseIdentifier];
}
#pragma mark: - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.clearAreaList.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DHButtonCell *btnCell = [collectionView dequeueReusableCellWithReuseIdentifier:buttonCellID forIndexPath:indexPath];
    btnCell.clearArea = self.clearAreaList[indexPath.row];
    return btnCell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    //[self.clearAreaView registerClass:[DHButtonCell class] forCellWithReuseIdentifier:buttonCellID];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    self.contentView.translatesAutoresizingMaskIntoConstraints = false;
//    
//    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
//                                                                 attribute:NSLayoutAttributeTop
//                                                                 relatedBy:NSLayoutRelationEqual
//                                                                    toItem:self.clearAreaView
//                                                                 attribute:NSLayoutAttributeTop
//                                                                multiplier:1.0 constant:0]];
//    
//    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
//                                                                 attribute:NSLayoutAttributeLeft
//                                                                 relatedBy:NSLayoutRelationEqual
//                                                                    toItem:self.clearAreaView
//                                                                 attribute:NSLayoutAttributeLeft
//                                                                multiplier:1.0 constant:0]];
//    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
//                                                                 attribute:NSLayoutAttributeRight
//                                                                 relatedBy:NSLayoutRelationEqual
//                                                                    toItem:self.clearAreaView
//                                                                 attribute:NSLayoutAttributeRight
//                                                                multiplier:1.0 constant:0]];
//    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
//                                                                 attribute:NSLayoutAttributeBottom
//                                                                 relatedBy:NSLayoutRelationEqual
//                                                                    toItem:self.clearAreaView
//                                                                 attribute:NSLayoutAttributeBottom
//                                                                multiplier:1.0 constant:0]];
    
}
@end
